package com.pass.util.dialog;

import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.github.rahatarmanahmed.cpv.CircularProgressView;
import com.pass.util.R;


import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 超时，并倒计时对话框
 */
public class TimeoutDialog extends BaseDialogFragment {
    private final static int MAX_COUNT_SEC = 20;
    private TextView titleView;
    private final DialogCallback callback;
    private RelativeLayout allLayout;
    private final Timer mTimer = new Timer();
    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private CountTimerTask mTimerTask;
    private CircularProgressView mCircularProgressView;

            //progress_view
    static class CountTimerTask extends TimerTask {

        private final WeakReference<TimeoutDialog> timeoutDialogRef;
        private final WeakReference<Handler> handler;
        private int lastSec;


        public CountTimerTask(TimeoutDialog timeoutDialog, Handler handler, int lastSec) {
            timeoutDialogRef = new WeakReference<>(timeoutDialog);
            this.handler = new WeakReference<>(handler);
            this.lastSec = lastSec;
        }

        @Override
        public void run() {
            if (null == timeoutDialogRef.get() || null == handler.get()) {
                return;
            }
            handler.get().post(() -> {
                lastSec -= 1;

                timeoutDialogRef.get().titleView.setText(String.format(Locale.CHINA, "%ds", lastSec));
                timeoutDialogRef.get().mCircularProgressView.setProgress(lastSec);
                if (lastSec == 0) {
                    timeoutDialogRef.get().callback.onCancelled();
                    timeoutDialogRef.get().dismiss();
                }
            });
        }
    }


    public TimeoutDialog(DialogCallback dialogCallback) {
        super(DialogPosition.CENTER, true);
        callback = dialogCallback;
        mTimerTask = new CountTimerTask(this, mHandler, MAX_COUNT_SEC);
        mTimer.schedule(mTimerTask, 1000, 1000);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dlg_timeout;
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.MyDialogStyle;
    }

    @Override
    protected void initView(View rootView) {
        mCircularProgressView = rootView.findViewById(R.id.progress_view);
        mCircularProgressView.setMaxProgress(MAX_COUNT_SEC);
        mCircularProgressView.setProgress(MAX_COUNT_SEC);
        titleView = rootView.findViewById(R.id.tv_sec);
        allLayout = rootView.findViewById(R.id.rl_total);
        titleView.setText(String.format(Locale.CHINA, "%ds", MAX_COUNT_SEC));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        allLayout.setOnClickListener(v -> {
            callback.onOKClicked();
            dismiss();
        });
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        super.onCancel(dialog);
        callback.onOKClicked();
    }

    @Override
    public void onDestroy() {
        mTimerTask.cancel();
        mTimer.cancel();
        mHandler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }
}
